/**
 * Created by sun on 2017/3/28.
 */
define(function (require) {
    var app = require('/../js/app');
    require('ui-table');

    var toastr =require('toastr');

    app.useModule("ui.table");


    var datepicker = require('datepicker');
    app.controller('packMan_listCtrl', ['$scope','$http','$filter' ,function ($scope, $http,$filter) {
        $scope.ctrlScope=$scope;
        $scope.add={};
        $scope.orderParams = {
            url: "/admin/asset/getPackageList"
        };
        //债权投资记录
        $scope.orderParams2 = {
            url: "/admin/asset/getItemListByPackageId"
        };
        datepicker($scope);
        //全局查询
        $scope.search = function() {
            $scope.ctrlScope.itemsearch.beginDateTime= $scope.ctrlScope.itemsearch.beginDateTimex;
            $scope.ctrlScope.itemsearch.endDateTime= $scope.ctrlScope.itemsearch.endDateTimex;
            $scope.ctrlScope.itemsearch.beginDateTime=$filter('date')($scope.ctrlScope.itemsearch.beginDateTime,'yyyy-MM-dd');
            $scope.ctrlScope.itemsearch.endDateTime=$filter('date')($scope.ctrlScope.itemsearch.endDateTime,'yyyy-MM-dd');
            //表格查询参数设置
            $scope.orderParams.ajaxParam=$scope.ctrlScope.itemsearch;
            //表格刷新
            $scope.orderParams.reload();
        };
        //全局查询重置
        $scope.reset=function(){
            $scope.orderParams.ajaxParam={};
            $scope.ctrlScope.itemsearch={};
            $scope.orderParams.reload();
        };
        //导出
        $scope.download=function(){
            var params=$scope.ctrlScope.itemsearch;
            $http.post("/admin/invest/excel/invest",params)
                .success(function (data){
                    if(data.result=="success"){
                        window.open(data.file);
                    }else{
                        toastr.error("导出失败");
                    }
                }).error(function(data){

                });
        };
        //查看包
        $scope.modifyModal=function(id){
            $scope.showPage = !$scope.showPage;
            //债权基本信息
            $http({
                method: 'POST',
                url: "/admin/asset/getItemListByPackageId",
                data: {
                    packageId:id
                }
            }).success(function(data){
              $scope.pageListInt=data.list;

            })
        }
        //查看标详情
        $scope.modifyModal3=function(id) {
            $scope.modelInit();
            $scope.showInt = !$scope.showInt;


            $scope.getModalItem(id);
            $scope.getModalInvest(id);
            $scope.getModalControl(id);
            $scope.getModalLoan(id);
            $scope.downloadId=id;

        }
        //新建
        $scope.addOrderModal=function(){
            $scope.vaildcheck={};
            $scope.checkArr=[];
            $scope.add.myPageName=undefined;
            $scope.newInstall = !$scope.newInstall;
        }
        //添加包

        $scope.installPage=function(){
            if($scope.add.myPageName==""||$scope.add.myPageName==undefined){
                $scope.installIs=false;
                toastr.error("请填写包名称");
                return;
            }else if($scope.checkArr==""){
                toastr.error("请勾选要选择的标");
                return;
            }
            $http({
                method: 'POST',
                url: "/admin/asset/addAssetPackage",
                data:{
                    packageName:$scope.add.myPageName,
                    itemStr:$scope.checkArr.toString()
                }
            }).success(function(data) {
                if(data.result=="fail"){
                    toastr.error(data.messageText);
                    return;
                }
                $scope.newInstall = !$scope.newInstall;
                $scope.orderParams.reload();
                $scope.orderParams2.reload();
            }).error(function(data) {
            });

        }
        //记录新建包勾选记录
        $scope.removeByValue=function(arr, val) {
            for(var i=0; i<arr.length; i++) {
                if(arr[i] == val) {
                    arr.splice(i, 1);
                    break;
                }
            }
        };
        $scope.checkArr=[];
        $scope.vaildcheck={};
        $scope.addcheck=function(id){
            $scope.vaildcheck[id]=! $scope.vaildcheck[id];
            if($scope.vaildcheck[id]==true){
                $scope.checkArr.push(id);
            }else{
                $scope.removeByValue($scope.checkArr, id);
            }
        };

        //下载合同
        $scope.getContract=function(url){
            if(url){
                window.open(url);
            }else{
                toastr.error("下载失败");
            }
        };
        //查询项目信息
        $scope.getModalItem = function(id){
            $http({
                method: 'POST',
                url: "/admin/item/getOneById",
                data: {
                    id: id
                }
            }).success(function(data) {
                console.log(data);

                $scope.ctrlScope.item= data.item;
                $scope.ctrlScope.item.loanUser=data.loanUser;
                $scope.ctrlScope.item.guaranteeUser=data.guaranteeUser;


                if(data.item.itemSingleMinInvestment==0){
                    $scope.ctrlScope.item.itemSingleMinInvestment="不设起投";
                }else{
                    $scope.ctrlScope.item.itemSingleMinInvestment=data.item.itemSingleMinInvestment;
                }
                if(data.item.itemMaxInvestment==0){
                    $scope.ctrlScope.item.itemMaxInvestment="不设上限";
                }else{
                    $scope.ctrlScope.item.itemMaxInvestment=data.item.itemMaxInvestment;
                }

                if(data.item.releaseTime===null){
                    $scope.ctrlScope.item.releaseTime='非定时标';
                }else{
                    $scope.ctrlScope.item.releaseTime=$filter('date')(data.item.releaseTime, 'yyyy-MM-dd HH:mm:ss')
                }

                $scope.ctrlScope.time= data.time;

                if($scope.ctrlScope.item.prepayment==1) {
                    if (data.itemPrepayment.partialPrepayment == 1) {
                        $scope.ctrlScope.item.partialPrepayment = 1
                    } else {
                        $scope.ctrlScope.item.partialPrepayment = 0
                    }
                    if (data.itemPrepayment.allPrepayment == 1) {
                        $scope.ctrlScope.item.allPrepayment = 1
                    } else {
                        $scope.ctrlScope.item.allPrepayment = 0
                    }
                }else{

                }


                if(data.itemPrepayment){
                    if(data.itemPrepayment.repaymentAudit==0){
                        $scope.ctrlScope.repay.repaymentAudit1=0;

                    }else{
                        $scope.ctrlScope.repay.repaymentAudit1=1;
                        $scope.ctrlScope.item.repaymentAudit=data.itemPrepayment.repaymentAudit;
                    }
                }

                $scope.personalImgs=[];
                $scope.carImgs=[];
                $scope.procedureImgs=[];
                $scope.riskImgs=[];
                $scope.skillImgs=[];
                for(var i=0;i<data.pictureList.length;i++){
                    if(data.pictureList[i].itemPictureType==1){
                        $scope.personalImgs.push(data.pictureList[i]);
                    }else if(data.pictureList[i].itemPictureType==2){
                        $scope.carImgs.push(data.pictureList[i]);
                    }else if(data.pictureList[i].itemPictureType==3){
                        $scope.procedureImgs.push(data.pictureList[i]);
                    }else if(data.pictureList[i].itemPictureType==4){
                        $scope.riskImgs.push(data.pictureList[i]);
                    }else if(data.pictureList[i].itemPictureType==5){
                        $scope.skillImgs.push(data.pictureList[i]);
                    }
                }
            }).error(function(data) {
            });
        };

       
        //查询投资信息
        $scope.orderParams1 = {
            url: "/admin/asset/getInvestListByItemId"
        }
        $scope.getModalInvest = function(id) {
            $scope.orderParams1.ajaxParam = {id:id};
            $scope.orderParams1.reload();
        };
        //弹框内容初始化
        $scope.modelInit=function(){
            $scope.imgs = [];
            $scope.score={};
            $scope.imgs1=[];
            $scope.imgs2=[];
            $scope.imgs3=[];
            $scope.imgs4=[];
            $scope.imgs5=[];
            $scope.imgs6=[];
            $scope.imgs7=[];
            $scope.imgs8=[];
            $scope.imgs21=[];
            $scope.imgs22=[];
            $scope.imgs23=[];
            $scope.imgs24=[];
            $scope.imgs25=[];
            $scope.imgs26=[];
            $scope.imgs27=[];
            $scope.imgs28=[];
            $scope.imgs29=[];
            $scope.imgs30=[];
            $scope.imgs41=[];
            $scope.imgs42=[];
            $scope.imgs43=[];
            $scope.imgs44=[];
            $scope.imgs45=[];
            $scope.imgs46=[];
            $scope.imgs47=[];
            $scope.imgs48=[];
            $scope.imgs61=[];
            $scope.imgs62=[];
            $scope.imgs63=[];
            $scope.imgs64=[];
            $scope.imgs65=[];
            $scope.ctrlScope.first1={};
            $scope.carLoadPics=[];
            $scope.ctrlScope.repay={};
            $scope.ctrlScope.item={};
            $scope.ctrlScope.message={};
            $scope.ctrlScope.message_2={};
            $scope.ctrlScope.car={};
            $scope.ctrlScope.training={};
            $scope.ctrlScope.CarMall={};
            $scope.ctrlScope.item.itemIsrecommend="0";
            $scope.ctrlScope.item.moveVip="0";
            $scope.ctrlScope.item.itemType="3";
            $scope.ctrlScope.item.itemIsnew="0";
            $scope.ctrlScope.item.itemCycle="30";
            $scope.ctrlScope.item.itemSingleMinInvestment="100";
            $scope.ctrlScope.item.itemMaxInvestment="0";
            $scope.ctrlScope.item.prepayment="0";
            $scope.ctrlScope.item.itemIsAuto="1";
            $scope.ctrlScope.item.debtTransferable="0";
            $scope.ctrlScope.item.itemLoanUse="短期周转";

        };
    }]);


});